package meituan;

import org.apache.zookeeper.*;

/**
 * 商家服务类
 */
public class ShopServer {
    // zookerrper的ip和端口
    private String connectString = "";

    private int timeOut = 60*1000;

    private ZooKeeper zooKeeper;

    // 连接zookeeper
    public void connect() throws Exception{
        zooKeeper = new ZooKeeper(connectString, timeOut, new Watcher() {
            public void process(WatchedEvent watchedEvent) {

            }
        });
    }
    // 创建节点
    public void register(String shopName) throws KeeperException, InterruptedException {
        // 该美团的跟节点必须存在，否则报错
        // EPHEMERAL_SEQUENTIAL 一定要创建临时有序的节点，一来可以自动编号，二来断开时可以自动打烊
        zooKeeper.create("/meituan/shop",shopName.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
    }


    public static void main(String[] args) throws Exception {
        // 1.我要开饭店
        ShopServer shop = new ShopServer();

        // 2.连接zookeeper
        shop.connect();

        // 3.注册到zookeeper
        shop.register(args[0]);

        // 4.处理业务逻辑代码
        shop.business(args[0]);
    }

    private void business(String shopName) {
        System.out.println(shopName+"正在营业！！");
    }
}
